package config;

import bean.MessageBean;
import bean.RoomBean;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import listener.AppConnectListener;
import listener.AppDisconnectListener;
import listener.RoomListener;
import listener.SendMessageListener;
import test.SendPlcData;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description
 * @Author szw
 * @Data 2019-12-25 13:50
 * @Version 1.0
 */

public class Server implements Runnable{

    private final SocketIOServer server;

    private List<SocketIOClient> clientList = new ArrayList<>();

    public Server(SocketIOServer socketIOServer) {
        this.server = socketIOServer;
    }

    public void init() {
        // 连接监听器
        server.addConnectListener(new AppConnectListener(server));
        // 断开连接监听器
        server.addDisconnectListener(new AppDisconnectListener(server));
        // 消息发送监听器
        server.addEventListener(EventConfig.CHATEVENT, MessageBean.class, new SendMessageListener(server));
        server.addEventListener(EventConfig.ROOMEVENT, RoomBean.class, new RoomListener(server,clientList));
        // 服务启动
        System.out.println("socket服务准备启动");
        server.startAsync();
        System.out.println("socket启动完成");
    }

    @Override
    public void run() {
        this.init();
        SendPlcData sendPlcData = new SendPlcData(server,clientList);
        sendPlcData.run();
    }
}
